﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using Box2D.XNA;
using AAEngine.Engine;
using AAEngine.Rendering2D;
using AAEngine.Rendering2D.Renderers;

namespace AAEngine.Box2D
{
    /// <summary>
    /// Helper component to visualize Box2D debug state. Properties let you
    /// toggle what is drawn.
    /// </summary>
    public class Box2DDebugComponent : RendererBase
    {
        private Box2DManagerComponent _manager;
        private DebugDrawer _drawer = new DebugDrawer();
        private bool _drawShapes = true;
        private bool _drawJoints = true;
        //private bool _drawCoreShapes = false;
        private bool _drawAABB = false;
        //private bool _drawOBB = false;
        private bool _drawPairs = false;
        private bool _drawCenterOfMass = false;

        public bool DrawShapes 
        {
            get { return _drawShapes; }
            set
            {
                _drawShapes = value;
                ApplyDebugFlags();
            }
        }

        public bool DrawJoints
        {
            get { return _drawJoints; }
            set
            {
                _drawJoints = value;
                ApplyDebugFlags();
            }
        }

        //public bool DrawCoreShapes
        //{
        //    get { return _drawCoreShapes; }
        //    set
        //    {
        //        _drawCoreShapes = value;
        //        ApplyDebugFlags();
        //    }
        //}

        public bool DrawAABB
        {
            get { return _drawAABB; }
            set
            {
                _drawAABB = value;
                ApplyDebugFlags();
            }
        }

        //public bool DrawOBB
        //{
        //    get { return _drawOBB; }
        //    set
        //    {
        //        _drawOBB = value;
        //        ApplyDebugFlags();
        //    }
        //}

        public bool DrawPairs
        {
            get { return _drawPairs; }
            set
            {
                _drawPairs = value;
                ApplyDebugFlags();
            }
        }

        public bool DrawCenterOfMass
        {
            get { return _drawCenterOfMass; }
            set
            {
                _drawCenterOfMass = value;
                ApplyDebugFlags();
            }
        }

        public Box2DManagerComponent SpatialManager
        {
            get { return _manager; }
            set { _manager = value; }
        }

        public override void OnAdd()
        {
			base.OnAdd();

            ApplyDebugFlags();
        }

        public override void OnReset() 
        {
            if (_manager != null)
            {
                _manager.SetDebugDrawer(_drawer);
            }
        }

        public void ApplyDebugFlags()
        {
            if (_drawShapes)
            {
                _drawer.AppendFlags(DebugDrawFlags.Shape);
            }
            else
            {
                _drawer.ClearFlags(DebugDrawFlags.Shape);
            }
            if (_drawJoints)
            {
                _drawer.AppendFlags(DebugDrawFlags.Joint);
            }
            else
            {
                _drawer.ClearFlags(DebugDrawFlags.Joint);
            }
            if (_drawPairs)
            {
                _drawer.AppendFlags(DebugDrawFlags.Pair);
            }
            else
            {
                _drawer.ClearFlags(DebugDrawFlags.Pair);
            }
            if (_drawAABB)
            {
                _drawer.AppendFlags(DebugDrawFlags.AABB);
            }
            else
            {
                _drawer.ClearFlags(DebugDrawFlags.AABB);
            }
            if (_drawCenterOfMass)
            {
                _drawer.AppendFlags(DebugDrawFlags.CenterOfMass);
            }
            else
            {
                _drawer.ClearFlags(DebugDrawFlags.CenterOfMass);
            }
        }

        public override int LayerIndex
        {
            get
            {
                if (Scene != null)
                {
                    return Scene.LayerCount;
                }
                return 0;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void Render(Box worldExtents, RenderQueue renderQueue)
        {
            _drawer.RenderQueue = renderQueue;
            _manager.World.DrawDebugData();
        }

        public override Box WorldExtents
        {
            get { return new Box(-float.MaxValue * 0.5f, -float.MaxValue * 0.5f, float.MaxValue, float.MaxValue); }
        }
    }
}
